<?php
namespace App\Admin\Logic\Dhe;

use App\Admin\Logic\GoodsLogic;
use App\Http\Logic\BaseLogic;
use App\Models\SwtDheGood;
use App\Services\DaHuangAdminService;

class DheGoodLogic extends BaseLogic {


    /**
     * @notes 获取大黄鹅商品列表
     * @param $start_time
     * @param $end_time
     * @return mixed
     * @throws \Illuminate\Http\Client\RequestException
     * @author Evil丿
     * @date 2024/06/21 17:07
     */
    public static function getGoodList($start_time = "", $end_time = "") {
        $list = DaHuangAdminService::getGoodsList($start_time, $end_time);
        return $list;
    }


    /**
     * @notes 刷新本地大黄鹅商品列表
     * @return string
     * @throws \Illuminate\Http\Client\RequestException
     * @author Evil丿
     * @date 2024/06/20 10:56
     */
    public static function refreshGoodList() {
        $first_time = "";

        $end_time = "";

        $created_at = SwtDheGood::latest()->value('created_at');

        if($created_at) {
            $first_time = $created_at->format('Y-m-d H:i:s');

            $end_time =  date('Y-m-d H:i:s', time());
        }

        $list = self::getGoodList($first_time, $end_time);

        $add_num = 0;

        $update_num = 0;

        foreach ($list as $item) {
            $data = [
                'bar_code'          => $item['barcode'],
                'goods_name'        => $item['skuSubject'],
                'goods_package'     => $item['skuPackageType'],
                'weight'            => $item['skuSize'],
                'cost_price'        => $item['skuCostPrice'],
                'sell_price'        => $item['skuSellingPrice']
            ];

            $goods_info = SwtDheGood::firstWhere('sku_id', $item['skuId']);

            if($goods_info) {
                $res = SwtDheGood::where('id', $goods_info -> id) -> update($data);
                $res && $update_num ++;
            } else {
                $data['sku_id'] = $item['skuId'];
                $res = SwtDheGood::create($data);
                $res && $add_num ++;
            }
        }

        return '已成功新增'.$add_num.'条数据，修改'.$update_num.'条数据，共计'.count($list);
    }

    public static function asyncGoodsToWarehouse($ids) {
        $goods = SwtDheGood::whereIn('id', $ids)->get();

        if(empty($goods)) {
            self::$error = "商品不存在";
            return false;
        }

        $num = 0;

        foreach($goods as $good) {
            $data = [
                'bar_code'      => $good['bar_code'],
                'goods_name'    => $good['with_name'] ?? $good['goods_name'],
                'goods_package' => $good['goods_package'],
                'weight'        => $good['weight'],
                'cost_price'    => $good['cost_price'],
                'sell_price'    => $good['sell_price'],
                'source_type'   => 1,
                'created_at'    => date('Y-m-d H:i:s', time()),
                'updated_at'    => date('Y-m-d H:i:s', time())
            ];

            $res = GoodsLogic::editGoods($data, $good['sku_id']);
            if($res) {
                SwtDheGood::where('id', $good['id']) -> update(['goods_id' => $res]);
                $num ++;
            }
        }

        return $num;
    }
}
